package com.dopies.bsdmall.activity;




import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dopies.BeeFramework.activity.BaseActivity;
import com.dopies.BeeFramework.model.BusinessResponse;
import com.dopies.BeeFramework.view.ToastView;
import com.dopies.bsdmall.R;
import com.dopies.bsdmall.adapter.ShopNotifityAdapter;
import com.dopies.bsdmall.model.MsgModel;
import com.dopies.bsdmall.model.ProtocolConst;
import com.dopies.bsdmall.protocol.FILTER;
import com.dopies.bsdmall.protocol.MESSAGE;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.external.maxwin.view.XListView.IXListViewListener;

public class ShopNotifyActivity extends BaseActivity implements IXListViewListener,BusinessResponse {

	private TextView title;
	private ImageView back;
	
	private XListView xlistView;
	private ShopNotifityAdapter shopNotifyAdapter;
	
	private MsgModel dataModel;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_notify);

        Resources resource = getBaseContext().getResources();
        String mes=resource.getString(R.string.profile_message);
		title = (TextView) findViewById(R.id.top_view_text);
		title.setText(mes);
		
		back = (ImageView) findViewById(R.id.top_view_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		xlistView = (XListView) findViewById(R.id.shop_notify_list);
		xlistView.setPullLoadEnable(true);
        xlistView.setPullRefreshEnable(true);
		xlistView.setRefreshTime();
		xlistView.setXListViewListener(this,1);
		
		dataModel = MsgModel.getInstance();
        dataModel.addResponseListener(this);
        dataModel.fetchPre();
        
        xlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MESSAGE message = dataModel.msg_list.get(position - 1);
                if (0 == message.action.compareTo("search"))
                {
                    String parameter = message.parameter;

                    Intent it = new Intent(ShopNotifyActivity.this, GoodsListActivity.class);
                    FILTER filter = new FILTER();
                    filter.keywords = parameter;
                    try
                    {
                        it.putExtra(GoodsListActivity.FILTER,filter.toJson().toString());
                    }
                    catch (JSONException e)
                    {

                    }

                    startActivity(it);
                }
            }
        });
//        setCont();
	}

	@Override
	public void onRefresh(int id) {
		
		dataModel.fetchPre();
		
	}

	@Override
	public void onLoadMore(int id) {
		dataModel.fetchNext();
		
	}
	
	public void setCont() {
		if(dataModel.msg_list.size() > 0 ) {
			xlistView.setVisibility(View.VISIBLE);
			if(shopNotifyAdapter == null) {
				shopNotifyAdapter = new ShopNotifityAdapter(this, dataModel.msg_list);
				xlistView.setAdapter(shopNotifyAdapter);
			} else {
				shopNotifyAdapter.list = dataModel.msg_list;
				shopNotifyAdapter.notifyDataSetChanged();
			}
		} else {
			xlistView.setVisibility(View.GONE);
            Resources resource = getBaseContext().getResources();
            String noc=resource.getString(R.string.no_notification);
            ToastView toast = new ToastView(this, noc);
	        toast.setGravity(Gravity.CENTER, 0, 0);
	        toast.show();
		}
	}

    @Override
    public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status) throws JSONException
    {
       if(null == url)
    	   return ;
       xlistView.stopRefresh();
       xlistView.stopLoadMore();
       if(url.endsWith(ProtocolConst.SHOP_NOTICE)){
    	   xlistView.setRefreshTime();
    	   setCont();
       }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dataModel.removeResponseListener(this);
    }
}
